package stark.app.function.imgocr;

import android.net.Uri;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.imgocr.ImgOcrEntry;
import com.stark.imgocr.api.Config;
import com.stark.imgocr.api.IRetCallback;
import com.stark.imgocr.api.OcrReqManager;
import com.stark.imgocr.api.bean.OcrRetBean;
import com.stark.picselect.PhotoSelectorBuilder;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.interfaces.OnSelectResultListener;

import java.util.List;

import androidx.annotation.Nullable;
import stark.app.R;
import stark.app.databinding.ActivityImgOcrDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;

/**
 * Author：daizhongbin on 2021/6/15
 * Email：1027154338@qq.com
 * Desc:
 */
public class ImgOcrDemoActivity extends BaseNoModelActivity<ActivityImgOcrDemoBinding> {

    /**请替换自己对应的APPCODE*/
    private static final String APPCODE = "a1116cd9edac40bfa231fb97ff835dd3";
    /**请替换自己对应的APPKEY*/
    private static final String APPKEY = "203951298";
    /**请替换自己对应的APPSECRET*/
    private static final String APPSECRET = "IImZknuutgJD5F8PaSjKFDb7PKoswO4m";
    private static final String NET_IMG_URL = "https://fegine-drug.oss-cn-shanghai.aliyuncs.com/image/urlimage.png";

    @Override
    protected int onCreate() {
        return R.layout.activity_img_ocr_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnAppcode.setOnClickListener(v -> {
            getPic(data -> {
                if (data != null && data.size() > 0) {
                    imgOcrByAppCode(data.get(0).getUri());
                }
            });
        });
        mDataBinding.btnAppsecret.setOnClickListener(v -> {
            imgOcrByAppSecret(NET_IMG_URL);
        });

        ImgOcrEntry.init(Config.verifyByAppSecret(APPKEY, APPSECRET));
        mDataBinding.btnGoDef.setOnClickListener(v -> {
            ImgOcrEntry.enterTakePic(this);
        });
    }

    @Override
    protected void initData() {

    }

    private void getPic(OnSelectResultListener<List<SelectMediaEntity>> listener) {
        PhotoSelectorBuilder.builder(this)
                .mode(PhotoConfig.Mode.PHOTO)
                .minCount(1)
                .maxCount(1)
                .listener(listener);
    }

    private void imgOcrByAppCode(String uriString) {
        showDialog("识别中...");
        Config config = Config.verifyByAppCode(APPCODE);
        OcrReqManager manager = new OcrReqManager(config);
        manager.urlImagesByUri(Uri.parse(uriString), new IRetCallback<List<OcrRetBean.Word>>() {
            @Override
            public void onRet(String errCode, String errMsg, @Nullable List<OcrRetBean.Word> wordList) {
                dismissDialog();
                handleRet(errCode, errMsg, wordList);
            }
        });
    }

    private void imgOcrByAppSecret(String url) {
        showDialog("识别中...");
        Config config = Config.verifyByAppSecret(APPKEY, APPSECRET);
        OcrReqManager manager = new OcrReqManager(config);
        manager.urlImages(url, new IRetCallback<List<OcrRetBean.Word>>() {
            @Override
            public void onRet(String errCode, String errMsg, @Nullable List<OcrRetBean.Word> wordList) {
                dismissDialog();
                handleRet(errCode, errMsg, wordList);
            }
        });
    }

    private void handleRet(String errCode, String errMsg, @Nullable List<OcrRetBean.Word> wordList) {
        if (wordList == null) {
            ToastUtils.showShort(errMsg);
            return;
        }
        StringBuilder stringBuilder = new StringBuilder();
        OcrRetBean.Word word = null;
        for (int i = 0; i < wordList.size(); i++) {
            word = wordList.get(i);
            stringBuilder.append(word.getWords());
            if (i != wordList.size() - 1) {
                stringBuilder.append("\n");
            }
        }
        mDataBinding.tvRet.setText(stringBuilder.toString());
    }

}
